package com.bite.friend.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bite.common.core.domain.TableDataInfo;
import com.bite.friend.domain.question.Question;
import com.bite.friend.domain.question.QuestionDetailVO;
import com.bite.friend.domain.question.QuestionQueryDTO;
import com.bite.friend.domain.question.es.QuestionES;
import com.bite.friend.domain.question.vo.QuestionVO;
import com.bite.friend.elasticsearch.QuestionRepository;
import com.bite.friend.manager.QuestionCacheManager;
import com.bite.friend.mapper.question.QuestionMapper;
import com.bite.friend.service.question.IQuestionService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    public QuestionRepository questionRepository;
    @Autowired
    public QuestionMapper questionMapper;
    //假如B端这个添加一个题目，那么他是否能获取呢?新增的数据怎么办呢
    @Autowired
    public QuestionCacheManager questionCacheManager;
    @Override
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
    long count= questionRepository.count(); //把索引对应的数量，来判断是否小于0
        if(count<=0){
            //假如es查不到，同步一下数据库
            refreshQuestion();
        }
        Sort sort=Sort.by(Sort.Direction.DESC,"createTime");
        Pageable pageable= PageRequest.of(questionQueryDTO.getPageNum()-1,questionQueryDTO.getPageSize(),sort);
        Integer difficulty=questionQueryDTO.getDifficulty();
        String keyword=questionQueryDTO.getKeyword();
        Page<QuestionES> questionESPage=null;
        if(difficulty==null&& StrUtil.isEmpty(keyword)){
         questionESPage=  questionRepository.findAll(pageable);
        }else if(StrUtil.isEmpty(keyword)){
           questionESPage= questionRepository.findQuestionByDifficulty(difficulty,pageable);
        }else if(difficulty==null){
           questionESPage= questionRepository.findByTitleOrContent(keyword,keyword,pageable);
        }else{
           questionESPage=questionRepository.findByTitleOrContentAndDifficulty(keyword,keyword,difficulty,pageable);
        }
        long total=questionESPage.getTotalElements();
        if(total==0){
            return TableDataInfo.empty();
        }
        List<QuestionES> questionESList = questionESPage.getContent();
        //因为他里面的东西很多，但是我们前端需要展示的没有那么多，所以生成一个
        List<QuestionVO> questionVOList=BeanUtil.copyToList(questionESList, QuestionVO.class);
        return TableDataInfo.success(questionVOList,total);
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        //通过这个方法，会返回
        QuestionES questionES=questionRepository.findById(questionId).orElse(null);
        QuestionDetailVO questionDetailVO=new QuestionDetailVO();
        if(questionES!=null){
            //此时es当中有数据
            BeanUtil.copyProperties(questionES,questionDetailVO);
            return questionDetailVO;
        }
        //从数据库里面查
        Question question=questionMapper.selectById(questionId);
        if(question==null){
            return null;
        }
        //刷新一下缓存
        refreshQuestion();
        BeanUtil.copyProperties(question,questionDetailVO);
        return  questionDetailVO;
    }

    @Override
    public String preQuestion(Long questionId) {
        Long listSize= questionCacheManager.getListSize();
        if(listSize==null||listSize<=0){
            questionCacheManager.refreshCache();
        }
       return questionCacheManager.preQuestion( questionId).toString();
    }

    @Override
    public String nextQuestion(Long questionId) {
        Long listSize= questionCacheManager.getListSize();
        if(listSize==null||listSize<=0){
            questionCacheManager.refreshCache();
        }
        return questionCacheManager.nextQuestion( questionId).toString();
    }


    private void refreshQuestion() {
        List<Question>questionList=questionMapper.selectList(new LambdaQueryWrapper<Question>());
        //假如没查出来就说明mysql没有数据
        if(CollectionUtil.isEmpty(questionList)){
            return ;
        }
        //同步给es,应该使用es的实体类
        List<QuestionES>questionESList =BeanUtil.copyToList(questionList, QuestionES.class);
        questionRepository.saveAll(questionESList);
    }

}
